import datetime

from flask import Blueprint, render_template, request
from models import MonitoringTask, MonitoredVideo, Comment
from utils.DataVisualization import VisualizationTool
import pandas as pd
from pyecharts.charts import Bar, Pie, WordCloud, Line, Page

data_analysis_bp = Blueprint("analysis", __name__, url_prefix="/analysis")


@data_analysis_bp.route("/")
def views():
    return render_template('data_analysis.html')


@data_analysis_bp.route("/get_line_by_time/")
def get_line_by_time():
    # 获取前端传递的日期参数
    date = request.args.get('date')

    # 将日期字符串转换为 datetime 对象
    selected_date = datetime.datetime.strptime(date, '%Y-%m-%d')

    # 计算当日的开始时间和结束时间
    start_of_day = selected_date.replace(hour=0, minute=0, second=0)
    end_of_day = selected_date.replace(hour=23, minute=59, second=59)

    # 查询 Comment 表数据，使用 between 方法来指定时间范围条件
    comments = Comment.query.filter(Comment.publish_time.between(start_of_day, end_of_day)).all()

    # 假设 VisualizationTool.line_for_comment_by_time 返回折线图数据
    line_data = VisualizationTool.line_for_comment_by_time(comments)

    # 返回折线图数据
    return line_data.dump_options_with_quotes()


@data_analysis_bp.route("/detail_line/<bvid>")
def video_detail_line(bvid):
    return render_template('video_line.html', bvid=bvid)


@data_analysis_bp.route("/detail_wordcount/<bvid>")
def video_detail_wordcount(bvid):
    return render_template('video_wordcount.html', bvid=bvid)


@data_analysis_bp.route("/get_line_chart/<bvid>")
def get_line_chart(bvid):
    comments = Comment.query.filter_by(bvid=bvid).all()
    line = VisualizationTool.line_for_comment_by_time(comments)
    return line.dump_options_with_quotes()


@data_analysis_bp.route("/get_wordcloud_chart/<bvid>")
def get_wordcloud_chart(bvid):
    comments = Comment.query.filter_by(bvid=bvid, is_violence=1).all()
    wordcound = VisualizationTool.violence_comment_word_cloud(comments)
    return wordcound.dump_options_with_quotes()



# @data_analysis_bp.route("/rank")
# def zone_rank():
#
#     render_template('rank.html')
    # zone_tasks = MonitoringTask.query.filter_by(task_type='zone').all()
    # for z in zone_tasks:




